package com.ampq;

import com.ampq.pojo.person;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * @Auther: fangym
 * @Date: 2018/9/12 17:03
 * @Description: rabbitmq发送消息 使用消息发送到exchange和发送到queue监听机制
 *               实现对应接口回调方法即可  一般发送成功之后进行业务处理
 */
@Component
public class QueueSend implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {
    @Autowired
    person per;

    @Autowired
    RabbitTemplate rabbitTemplate;

    public QueueSend(RabbitTemplate rabbitTemplate) {
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(this);
    }

    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        /*System.out.printf("correlationData:"+correlationData);
        System.out.println(ack);
        System.out.println("cause:"+cause);*/
    }

    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        /*System.out.println("message:" + message);
        System.out.println("replyCode:" + replyCode);
        System.out.println("replyText:" + replyText);
        System.out.println("exchange:" + exchange);
        System.out.println("routingKey:" + routingKey);*/
    }

    public void sendMessage(String Str){
        rabbitTemplate.convertAndSend("ExChange.direct","learn.emps",Str);
    }

}
